package com.godo.test.downfile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaiduBaikeImageDown extends DownImage {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException, IOException {
		HashSet<String> set = new HashSet<String>() ;
		String url = "http://tieba.baidu.com/p/1241352729" ;
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1250823508" ;
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1250952705" ;
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1250829374" ;
		set.add(url) ;
		url = "http://tieba.baidu.com/p/263949164";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/499925368" ;
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1085067218";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/495431047";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/511594286";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1123195264";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/263670168";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/274398248";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/351778880";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/512180021";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1064897828";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/700952518";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/503501203";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/267724230";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/267724230";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/263150767";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/275270737";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1251777792";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1251850949";//??????
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1127550419" ;
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1252027998";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1252859482";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1252918628";
		set.add(url) ;
		url = "http://tieba.baidu.com/p/1250719828";
		set.add(url);
		url = "http://tieba.baidu.com/p/1253217357";
		url = "http://tieba.baidu.com/p/1252743899";//???
		url = "http://tieba.baidu.com/p/1252700817";
		url = "http://tieba.baidu.com/p/1253391609";
		url = "http://tieba.baidu.com/p/1256964359";
		url = "http://tieba.baidu.com/p/1257008813";
		url = "http://tieba.baidu.com/p/1257030635";
		url = "http://tieba.baidu.com/p/1257173235";
		url = "http://tieba.baidu.com/p/1258109199";
		url = "http://tieba.baidu.com/p/1255906157";
		url = "http://tieba.baidu.com/p/1236572502";
		url = "http://tieba.baidu.com/p/1257108426";
		url = "http://tieba.baidu.com/p/1257108426";
		url = "http://tieba.baidu.com/p/1256599257";
		url = "http://tieba.baidu.com/p/1254955789";
		url = "http://tieba.baidu.com/p/1258314621";
		url = "http://tieba.baidu.com/p/1255126208";
		url = "http://tieba.baidu.com/p/1256653809";
		url = "http://tieba.baidu.com/p/1259147076";
		url = "http://tieba.baidu.com/p/1259178099";
		url = "http://tieba.baidu.com/p/1259181801";
		url = "http://tieba.baidu.com/p/1259405552";
		url = "http://tieba.baidu.com/p/1259455490";
		url = "http://tieba.baidu.com/p/1259362252";
		url = "http://tieba.baidu.com/p/1262446184";
		url = "http://tieba.baidu.com/p/1265839703";
		url = "http://tieba.baidu.com/p/1267610610";
		url = "http://tieba.baidu.com/p/1269486593";
		url = "http://tieba.baidu.com/p/1270057514";
		url = "http://tieba.baidu.com/p/1273628131";//?????
		url = "http://tieba.baidu.com/p/1266352272";
		url = "http://tieba.baidu.com/p/1273479015";
		url = "http://tieba.baidu.com/p/1264257068";
		url = "http://tieba.baidu.com/p/1275373283";
		url = "http://tieba.baidu.com/p/1275382631";
		url = "http://tieba.baidu.com/p/1284162026";
		url = "http://tieba.baidu.com/p/1284295386";
		url = "http://tieba.baidu.com/p/1285096559";
		url = "http://tieba.baidu.com/p/1285411581";
		url = "http://tieba.baidu.com/p/1286005165";
		url = "http://tieba.baidu.com/p/1282650335";
		url = "http://tieba.baidu.com/p/1286747234";
		url = "http://tieba.baidu.com/p/1291126844";
		url = "http://tieba.baidu.com/p/1291875375";
		url = "http://tieba.baidu.com/p/1283584444";
		url = "http://tieba.baidu.com/p/1291407756";
		url = "http://tieba.baidu.com/p/1292268345";
		url = "http://tieba.baidu.com/p/1292197179";
		url = "http://tieba.baidu.com/p/1292237583";
		url = "http://tieba.baidu.com/p/1291879827";
		url = "http://tieba.baidu.com/p/1291369447";
		url = "http://tieba.baidu.com/p/1292331199";
		url = "http://tieba.baidu.com/p/1292224450";
		url = "http://tieba.baidu.com/p/1291293401";
		url = "http://tieba.baidu.com/p/1293213601";
		url = "http://tieba.baidu.com/p/1261398399";
		url = "http://tieba.baidu.com/p/1294548615";
		url = "http://tieba.baidu.com/p/1295766832";
		url = "http://tieba.baidu.com/p/1296761543";
		url = "http://tieba.baidu.com/p/1295125486";
		url = "http://tieba.baidu.com/p/1296725562";
		url = "http://tieba.baidu.com/p/1296800495";
		url = "http://tieba.baidu.com/p/1295932680";
		url = "http://tieba.baidu.com/p/1296962920";
		url = "http://tieba.baidu.com/p/1296938342";
		url = "http://tieba.baidu.com/p/1296834020";
		url = "http://tieba.baidu.com/p/1296514322";
		//壁纸
		url = "http://tieba.baidu.com/p/1297118111";
		url = "http://tieba.baidu.com/p/687195897";
		url = "http://tieba.baidu.com/p/1173091960";
		url = "http://tieba.baidu.com/p/1075561004";
		url = "http://tieba.baidu.com/p/1230440491";
		url = "http://tieba.baidu.com/p/1231376628";
		url = "http://tieba.baidu.com/p/731862869";
		url = "http://tieba.baidu.com/p/1008972039";
		url = "http://tieba.baidu.com/p/1296328284";
		url = "http://tieba.baidu.com/p/1197020056";
		url = "http://tieba.baidu.com/p/1261919413";
		url = "http://tieba.baidu.com/p/1162653351";
		url = "http://tieba.baidu.com/p/1272031111";
		url = "http://tieba.baidu.com/p/918889590";
		url = "http://tieba.baidu.com/p/1242155224";
		url = "http://tieba.baidu.com/p/916329458";
		url = "http://tieba.baidu.com/p/1115933867";
		
		String out= "e:/data/img/";

		BaiduBaikeImageDown down = new BaiduBaikeImageDown() ;
		
//		int p=0;
//		for(String u:set){
//			System.out.println(p+++"==================="+u) ;
//			File outdir = down.getOutDir(url,new File(out)) ;
//			for(int i=1;i<10;i++){
//				System.out.println(u+"?pn="+i) ;
//				down.downImageFromUrl(u+"?pn="+i, outdir) ;
//			}
//		}
		
		File outdir = down.getOutDir(url,new File(out)) ;
		for(int i=1;i<20;i++){
			down.downImageFromUrl(url+"?pn="+i, outdir) ;
		}
	}
	
	public void downImageFromUrl(String url,String out,String cls) throws InterruptedException, IOException{
		ArrayList<String> list = getUrls(url , "gbk");

//		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HHmm");
//		String date = sf.format(new Date()) ;
		for(String u:list){
			System.err.println(u) ;
			File o = new File(out,cls) ;
			try {
				super.downImage(u, o) ;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Thread.sleep(200*1) ;
		}
	}
	
	public ArrayList<String> getUrls(String url,String code) throws IOException{
		ArrayList<String> list = new ArrayList<String>() ;
		String content = super.readUrlContent(url, code,5000) ;
		String rege = "src=\"(http://(.*?)\\.(jpg|JPG))\"" ;
		Pattern p = Pattern.compile(rege); 
//		用Pattern类的matcher()方法生成一个Matcher对象 
		Matcher m = p.matcher(content); 
		while(m.find()){
			list.add(m.group(1)) ;
		}
		return list ;
	}

	@Override
	public void downImageFromUrl(String url, File out) throws InterruptedException, IOException {
		ArrayList<String> list = getUrls(url , "gbk");

		for(String u:list){
			System.err.println(u) ;
			try {
				downImage(u, out) ;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Thread.sleep(200*1) ;
		}
	}

	@Override
	public File getOutDir(String url,File outDir) {
		String cls = url.substring(url.lastIndexOf("/")+1) ;
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HHmm");
		return new File(outDir,sf.format(new Date())+" "+cls) ;
	}

}
